package com.enjia.common.net;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by chenfeifei on 2017-08-17.
 */

public class RetrofitClient {
    /*HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);*/
    private static Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();//使用 gson coverter，统一日期请求格式

    private static OkHttpClient client = new OkHttpClient.Builder()
            .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .connectTimeout(10, TimeUnit.SECONDS)               //设置超时时间
            .readTimeout(10,TimeUnit.SECONDS)
            .writeTimeout(10,TimeUnit.SECONDS)
           // .addInterceptor(new HttpLogger1())
          /* .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    LogUtils.e("拦截器=======请求头="+request.headers().toString());
                    if(null!=request.body()){
                        LogUtils.e("拦截器====请求体===="+request.body().toString());
                    } else {
                        LogUtils.e("拦截器====请求体====" + "请求体为空");
                    }
                    Response response = chain.proceed(request);
                    //LogUtils.e("拦截器==响应头==="+response.headers().toString());
                    ResponseBody body = response.body();
                    LogUtils.e("拦截器==响应体数据==="+ body.string());

                    return response;
                }
            })*/
            .build();

    private static Retrofit retrofitClient = new Retrofit.Builder()
                .baseUrl(UrlManager.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(client)
                .build();

    /**
     *  获取Retrofit接口对象。
     * @return
     */
    public static ParentApi getService(){
        return retrofitClient.create(ParentApi.class);
    }

    static class HttpLogger implements HttpLoggingInterceptor.Logger {
        @Override
        public void log(String message) {
            Log.e("okhttp",message);
        }
    }
}
